import { setToken, getToken } from '@/libs/util'
import { loginUser } from '@/api/medical.js'
export default {
  state: {
    severUserInfo: {},
    token: getToken()
  },
  getters: {
    userInfo: state => state.severUserInfo
  },
  mutations: {
    setSeverUserInfo (state, severUserInfo) {
      state.severUserInfo = severUserInfo
    },
    setToken (state, token) {
      state.token = token
      setToken(token)
    }
  },
  actions: {
    userLogin ({ commit }, { tel, password, userType }) {
      return new Promise((resolve, reject) => {
        loginUser({ tel, password, userType }).then(res => {
          if (res.data.errcode === 0) {
            res.data.data.userType = userType
            commit('setToken', res.data.data.member_id)
            commit('setSeverUserInfo', res.data.data)
            resolve(res.data.data)
          } else {
            reject(res.data.errmsg)
          }
        }).catch(err => {
          reject(err)
        })
      })
    },
    // 退出登录
    handleLogOut ({ state, commit }) {
      return new Promise((resolve, reject) => {
        // logout().then(() => {
        //  commit('setToken', '')
        //  commit('setAccess', [])
        //  resolve()
        // }).catch(err => {
        //  reject(err)
        // })
        // 如果你的退出登录无需请求接口，则可以直接使用下面三行代码而无需使用logout调用接口
        commit('setToken', '')
        commit('setAccess', [])
        resolve()
      })
    }
  }
}
